<?php

namespace frontend\modules\user\models;

use Yii;
use frontend\modules\user\models\logic\Search;
use common\models\user\UserAccountOrange;
use common\models\user\UserAccountOrangeRecord;
use yii\data\ActiveDataProvider;

class CreditSearch extends Search {

    public function search($params = []) {
        $query = UserAccountOrangeRecord::find()
        ->alias('t')
        ->where(['user_id' => $this->user->id, 'deleted_at' => 0])
        ->select("id,t.`value` * t.`balance` as amount,mark,created_at")
        ->asArray()
        ->orderBy('created_at desc');

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        Yii::$app->db->cache(function ($db) use ($dataProvider) {
            $dataProvider->prepare();
        }, 60);

        return $dataProvider;
    }

    public function current() {
        return UserAccountOrange::find()->where(['user_id' => $this->user->id])->select(['current'])->one();
    }

}
